package me.zhengjie.modules.system.service.dto


import cn.hutool.core.bean.BeanUtil
import cn.hutool.core.bean.copier.CopyOptions
import com.alibaba.fastjson.annotation.JSONField
import com.fasterxml.jackson.annotation.JsonIgnore
import com.fasterxml.jackson.annotation.JsonInclude
import com.fasterxml.jackson.annotation.JsonTypeInfo
import com.fasterxml.jackson.annotation.JsonTypeName
import io.swagger.v3.oas.annotations.media.Schema
import me.zhengjie.common.base.BaseDTO
import me.zhengjie.common.utils.JacksonUtil
import java.sql.Date

/**
 * @author Kuki Wu
 * @date 2024-11-23
 */

@Schema(description = "用户dto类")
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS)
@JsonTypeName("UserDTO")
data class UserDTO(

    //@ApiModelProperty(hidden = true)
    @Schema(description = "用户ID", required = true, hidden=true)
    var id: Long? = null,

    @Schema(description = "角色", required = false, hidden=true)
    @get:JsonTypeInfo(use = JsonTypeInfo.Id.CLASS)
    @get:JsonInclude(JsonInclude.Include.NON_EMPTY)
    var roles: MutableSet<RoleSmallDTO>? = null,

    @Schema(description = "职业", required = false, hidden=true)
    @get:JsonTypeInfo(use = JsonTypeInfo.Id.CLASS)
    var job: MutableSet<JobSmallDTO>? = null,

    @Schema(description = "部门", required = false, hidden=true)
    @get:JsonTypeInfo(use = JsonTypeInfo.Id.CLASS)
    var dept: DeptSmallDTO? = null,


    @Schema(description = "部门ID", required = false)
    var deptId: Long? = null,

    @Schema(description = "用户名", required = true)
    var username: String? = "",

    @Schema(description = "昵称", required = true)
    var nickName: String? = "",

    @Schema(description = "邮箱", required = true)
    var email: String? = "",

    @Schema(description = "电话", required = true)
    var phone: String? = "",

    @Schema(description = "gender", required = true)
    var gender: String? = "",

    @Schema(description = "头像", required = true)
    var avatarName: String? = "",

    @Schema(description = "头像图片路径", required = true)
    var avatarPath: String? = "",

    @Schema(description = "是否激活", required = true)
    var enabled: Boolean? = true,

    @Schema(description = "密码", required = false)
    //@get:JsonIgnore
    @get:JSONField(serialize = false)
    var password: String? = "",

    @get:JSONField(serialize = false)
    var isAdmin: Boolean? = false,

    @Schema(description = "最后密码重置时间", required = false)
    var pwdResetTime: Date? = null
): BaseDTO(){
    annotation class Update
    /**
     * 从 JSON 字符串实例化对象
     */
    companion object {
        fun fromJson(json: String): UserDTO? {
            val obj=JacksonUtil.parseObject(json, UserDTO::class.java)!!
            return when(obj){
                is UserDTO -> obj
                else -> null
            }
        }
    }
    fun copy(source: UserDTO?) {
        BeanUtil.copyProperties(source, this, CopyOptions.create().setIgnoreNullValue(true))
    }
}